﻿using Shell.DomainDot;
using System.Web.Mvc;
using Framework.Poco;
using Framework.Domain;
using System;
using System.Collections.Generic;

namespace Framework.Mvc.Controllers
{
    /// <summary>
    /// 广告控制器
    /// </summary>
    public class AdvertisingController : Controller
    {
        private IDomainDotFactory factory;

        /// <summary>
        /// 创建器
        /// </summary>
        public AdvertisingController()
        {
            factory = DomainDotFactory.Factory;
        }

        /// <summary>
        /// 广告创建
        /// </summary>
        [HttpGet]
        public ActionResult AgencyGroupCreate(User currentUser)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var context = factory.Create<GlobalContext>();
                var model = new AdvertisingCreateViewModel();
                model.AgencyGroups = context.GetAllAgencyGroups();
                return View(model);
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 广告创建
        /// </summary>
        [HttpPost]
        public ActionResult AgencyGroupCreate(User currentUser, AdvertisingCreateViewModel model, FilesUploaded filesUploaded)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                if (filesUploaded != null && filesUploaded.Identities.Count == 1)
                {
                    var groupContext = factory.Create<AgencyGroupAdvertisingContext>(model.AgencyGroupIdentity);
                    groupContext.Create(model.Instance, filesUploaded.Identities[0]);
                    return this.RedirectToAction("List");
                }
                //TODO 这里应该返回错误信息(金辉)
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 文章分类下的广告创建
        /// </summary>
        [HttpGet]
        public ActionResult CategoryCreate(User currentUser)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var context = factory.Create<SelectCategory>();
                var model = new AdvertisingCreateViewModel();
                model.Categories = context.GetAllCategories();
                return View(model);
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 文章分类下的广告创建
        /// </summary>
        [HttpPost]
        public ActionResult CategoryCreate(User currentUser, AdvertisingCreateViewModel model, FilesUploaded filesUploaded)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                if (filesUploaded != null && filesUploaded.Identities.Count == 1)
                {
                    var categoryContext = factory.Create<CategoryAdvertisingContext>(model.CategoryIdentity);
                    categoryContext.Create(model.Instance, filesUploaded.Identities[0]);
                    return this.RedirectToAction("List");
                }
                //TODO 这里应该返回错误信息(金辉)
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 全局广告
        /// </summary>
        [HttpGet]
        public ActionResult GlobalCreate(User currentUser)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                return View();
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 全局广告
        /// </summary>
        [HttpPost]
        public ActionResult GlobalCreate(User currentUser, AdvertisingCreateViewModel model, FilesUploaded filesUploaded)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                if (filesUploaded != null && filesUploaded.Identities.Count == 1)
                {
                    var context = factory.Create<AdvertisingContext>();
                    context.Create(model.Instance, filesUploaded.Identities[0]);
                    return this.RedirectToAction("List");
                }
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 广告删除
        /// </summary>
        [HttpGet]
        public ActionResult Delete(User currentUser, string name, Guid advertisingId)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var globalContext = factory.Create<GlobalContext>();
                var agencyGroup = globalContext.FindAgencyGroup(name);
                var categoryContext = factory.Create<SelectCategory>();
                var category = categoryContext.FindCategory(name);
                var advertising = factory.Create<AdvertisingAndPictureContext>(advertisingId);
                var model = new AdvertisingDeleteViewModel();
                if (agencyGroup != null)
                {
                    model.AgencyGroupIdentity = agencyGroup.Id;
                }
                if (category != null)
                {
                    model.CategoryIdentity = category.Id;
                }
                model.AdvertisingIdentity = advertisingId;
                model.PictureIdentity = advertising.GetBinaryFileIdentity();
                return View(model);
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 广告删除
        /// </summary>
        [HttpPost]
        public ActionResult Delete(User currentUser, AdvertisingDeleteViewModel model)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                if (model.CategoryIdentity != Guid.Empty)
                {
                    var context = factory.Create<CategoryAdvertisingContext>(model.CategoryIdentity);
                    context.Delete(model.AdvertisingIdentity);
                }
                if (model.AgencyGroupIdentity != Guid.Empty)
                {
                    var context = factory.Create<AgencyGroupAdvertisingContext>(model.AgencyGroupIdentity);
                    context.Delete(model.AdvertisingIdentity);
                }
                var advertisingContext = factory.Create<AdvertisingContext>();
                advertisingContext.Delete(model.AdvertisingIdentity);
                return this.RedirectToAction("List");
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 广告列表
        /// </summary>
        public ActionResult List(User currentUser)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var context = factory.Create<SelectAdvertising>();
                var advertisings = context.GetAll();
                var list = new List<ArrangedImageAdvertising>();
                foreach (var item in advertisings)
                {
                    var picture = factory.Create<AdvertisingAndPictureContext>(item.Id);
                    var id = picture.GetBinaryFileIdentity();
                    var advertisingAndPictureId = new ArrangedImageAdvertising(item, id);
                    list.Add(advertisingAndPictureId);
                }
                var model = new AdvertisingListViewModel();
                model.List = list;
                return View(model);
            }
            return this.RedirectToAction("Index", "Home");
        }
    }
}
